package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppOrder;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.order.OrderSelectParam;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppOrderMapper;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 【请填写功能名称】实现类
 * @author LikeAdmin
 */
@Service
public class TAppOrderServiceImpl implements ITAppOrderService {
        
    @Resource
    TAppOrderMapper appOrderMapper;

    @Override
    public TAppOrder createDefaultOrder(TAppOrder order) {
        Date now = new Date();
        order.setCreateTime(now);
        order.setUpdateTime(now);
        order.setOrderStatus(1);
        order.setOrderPayStatus(1);
        return order;
    }

    @Override
    public void insert(TAppOrder order) {
        int i = appOrderMapper.insert(order);
    }

    @Override
    public String selectOrderList(OrderSelectParam param) {
        Integer orderType = param.getOrderType();
        Integer orderStatus = param.getOrderStatus();
        Integer userId = param.getUserId();
        if(userId==null){
            return JsonReturn.fail("必传参数不能为空",40001).toJsonStr();
        }
        QueryWrapper<TAppOrder> wrapper = new QueryWrapper<>();
        if(orderType!=null){
            wrapper.eq("order_type",orderType);
        }
        if(orderStatus!=null){
            wrapper.eq("order_status",orderStatus);
        }
        wrapper.eq("user_id",userId).ne("order_status",5);
        wrapper.orderByDesc("create_time");
        List<TAppOrder> orderList = appOrderMapper.selectList(wrapper);
        return JsonReturn.success().setListData(Optional.ofNullable(orderList).orElse(new ArrayList<>())).toJsonStr();
    }
}
